package com.hjqjl.whdemo.feature.feature0.ui.activity.surfaceview;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;

import com.hjqjl.whdemo.R;
import com.hjqjl.whdemo.base.BaseActivity;
import com.hjqjl.whdemo.databinding.ActivitySurfaceView1Binding;

import java.util.Timer;
import java.util.TimerTask;

//蝴蝶来回飞
public class SurfaceView1Activity extends BaseActivity {
    public static void start(Context context) {
        Intent starter = new Intent(context, SurfaceView1Activity.class);
        context.startActivity(starter);
    }

    //画蝴蝶，写的相当啰嗦，可优化
    private SurfaceView surfaceView;
    private SurfaceHolder surfaceHolder;
    private Bitmap butterflybitmap1;
    private Timer butterflytimer = new Timer();

    private int[] butterflyrightids1 = {R.drawable.butterfly_f01, R.drawable.butterfly_f02, R.drawable.butterfly_f03, R.drawable.butterfly_f04,
            R.drawable.butterfly_f05, R.drawable.butterfly_f06};
    private int[] butterflyleftids1 = {R.drawable.butterfly_zf01, R.drawable.butterfly_zf02, R.drawable.butterfly_zf03, R.drawable.butterfly_zf04,
            R.drawable.butterfly_zf05, R.drawable.butterfly_zf06};
    private int butterflyid1;// 蝴蝶飞时图片id编号(左右飞都是6个图片,就用了同一个编号)
    private Canvas canvas;

    private ActivitySurfaceView1Binding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivitySurfaceView1Binding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        // 动画效果
        surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
        surfaceView.setZOrderOnTop(true);
        surfaceView.getHolder().setFormat(PixelFormat.TRANSLUCENT);// 设置透明
        surfaceHolder = surfaceView.getHolder();

        binding.btnSurface.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 通过定制器控制每0.2秒运行一次TranslateAnimation动画
                butterflytimer.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        canvas = surfaceHolder.lockCanvas();
                        canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
                        butterfly1();
                        surfaceHolder.unlockCanvasAndPost(canvas);
                    }

                }, 1000, 100);// 延迟防止空指针异常,循环时间同时控制着翅膀煽动（图片帧改变）和移动快慢（可分开）
            }
        });
    }

    private float butterflyX1 = 0;
    private float butterflyY1;
    private boolean butterflyright1 = true;// true蝴蝶要向右飞
    private int butterflyIdRegulate1;
    private int butterStep = 30;

    private void butterfly1() {
        butterflyIdRegulate1++;
        if (butterflyIdRegulate1 % 3 == 0) {//控制翅膀煽动的快慢
            butterflyid1++;// 左右飞编号都一样，就不写两个了
            if (butterflyid1 > 5) {
                butterflyid1 = 0;
            }
        }
        if (butterflyX1 > 600) {
            butterflyright1 = false;
        }
        if (butterflyX1 <= 0) {
            butterflyright1 = true;
        }

        if (butterflyright1) {
            butterflyX1 += butterStep;
            butterflyY1 = 400 + (float) (Math.cos(butterflyX1 / 20) * 30);
            butterflybitmap1 = BitmapFactory.decodeResource(getResources(), butterflyrightids1[butterflyid1]);
        }
        if (!butterflyright1) {
            butterflyX1 -= butterStep;
            butterflyY1 = 400 + (float) (Math.sin(butterflyX1 / 20) * 30);
            butterflybitmap1 = BitmapFactory.decodeResource(getResources(), butterflyleftids1[butterflyid1]);
        }
//        Bitmap bitmap = ImageUtil.zoomBitmap(butterflybitmap1, 25, 25);
        canvas.drawBitmap(butterflybitmap1, butterflyX1, butterflyY1, null);
    }

    @Override
    protected void onDestroy() {
        butterflytimer.cancel();
        super.onDestroy();
    }
}